
#region using statements

using System;
using System.Data;
using System.Configuration;
using System.Web.UI;
//using mojoPortal.Web.Framework;
//using mojoPortal.Web.UI;
//using log4net;
//using Resources;
using System.Data.SqlClient;
using mojoPortal.Web.RS.AppCode.Lib;

#endregion

namespace mojoPortal.Web.RS.Modules
{

    public partial class RSMenuSiteMapControl : UserControl  //SiteModuleControl
    {
        public mojoPortal.Business.SiteUser siteUser;

        #region OnInit

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.Load += new EventHandler(Page_Load);

        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!Page.IsPostBack)
            //{
            SetRSMenuSiteMapProvider();
            LoadSettings();
            PopulateLabels();
            PopulateControls();
            //}
        }

        private void PopulateControls()
        {
            //TitleControl.EditUrl = SiteRoot + "/Empty/EmptyEdit.aspx";
            //TitleControl.Visible = !this.RenderInWebPartMode;
            //if (this.ModuleConfiguration != null)
            //{
            //    this.Title = this.ModuleConfiguration.ModuleTitle;
            //    this.Description = this.ModuleConfiguration.FeatureName;
            //}
        }

        private void PopulateLabels()
        {
            //TitleControl.EditText = "Edit";
        }

        private void LoadSettings()
        {
            //
        }

        public void SetRSMenuSiteMapProvider()
        {
            mojoPortal.Business.SiteUser siteUser = mojoPortal.Web.SiteUtils.GetCurrentSiteUser();
            if (siteUser != null && siteUser.MobilePin.Length > 0 && (SiteUtils.GetCurrentPageUrl().Contains("/RS/") || Request.Url.AbsolutePath.Contains("/RS/")))
            {
                RSSiteMapProvider rsSiteMapProvider = null;
                if (Session["RSSITEMAP_PROVIDER"] != null)
                {
                    rsSiteMapProvider = (RSSiteMapProvider)Session["RSSITEMAP_PROVIDER"];
                }
                else
                {
                    rsSiteMapProvider = GetRSSiteMapProvider(siteUser.MobilePin);
                    Session["RSSITEMAP_PROVIDER"] = rsSiteMapProvider;
                }


                if (rsSiteMapProvider != null && rsSiteMapProvider.siteMapProvider.Length > 0)
                {
                    mojoPortal.Web.UI.SiteTitle lblSiteTitle = (mojoPortal.Web.UI.SiteTitle)this.Parent.FindControl("SiteTitle");
                    if(lblSiteTitle != null)
                        lblSiteTitle.OverrideTitle = rsSiteMapProvider.companyName;

                    mojoPortal.Web.UI.WelcomeMessage lblWelcomeMessage = (mojoPortal.Web.UI.WelcomeMessage)this.Parent.FindControl("WelcomeMessage");
                    if(lblWelcomeMessage != null)
                        lblWelcomeMessage.OverrideFormat = rsSiteMapProvider.firstName + " " + rsSiteMapProvider.lastName;

                    lblReadyServiceSpecialHeader.Text = ""; //DateTime.Now.ToShortDateString();
                    RSSiteMapDataSource1.SiteMapProvider = rsSiteMapProvider.siteMapProvider;
                    divReadyServiceSiteMap.Visible = true;
                }
                else
                {
                    RSSiteMapDataSource1.SiteMapProvider = "";
                    divReadyServiceSiteMap.Visible = false;
                }
            }
        }

        public RSSiteMapProvider GetRSSiteMapProvider(string pin)
        {
            RSSiteMapProvider rsSiteMapProvider = null;
            String strSQL = "select c.companyid, e.FirstName, e.LastName, c.companyname, c.sitemapprovider from rs_tblEmployee e inner join rs_tblcompany c on e.companyid=c.companyid " +
                "where e.PIN=@pin and e.EnableLogin=1 and e.IsActive=1";

            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;
            SqlDataReader myDataReader = null;
            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ReadyServiceConnectionStringMain"].ToString());
            myCommand = new SqlCommand(strSQL, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.NVarChar;
            param.ParameterName = "@pin";
            param.Value = pin;
            myCommand.Parameters.Add(param);

            myConnection.Open();
            myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            if (myDataReader.HasRows)
            {
                myDataReader.Read();
                rsSiteMapProvider = new RSSiteMapProvider();
                rsSiteMapProvider.firstName = myDataReader["FirstName"].ToString();
                rsSiteMapProvider.lastName = myDataReader["LastName"].ToString();
                rsSiteMapProvider.companyName = myDataReader["CompanyName"].ToString();
                rsSiteMapProvider.siteMapProvider = myDataReader["SiteMapProvider"].ToString();
            }
            myConnection.Close();

            return rsSiteMapProvider;
        }
    }
}
